package com.fahai.web.service.impl;/**
 * Created by Lilinjun on 17/6/6.
 */

import com.fahai.common.PageVo;
import com.fahai.web.dao.DataVDao;
import com.fahai.web.entity.DataAnalyse;
import com.fahai.web.entity.DataAnalyseSum;
import com.fahai.web.service.DataAnalysisService;
import com.fahai.web.vo.DataAnalyseQueryVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据分析
 *
 * @author
 * @create 2017-06-06 下午5:38
 **/
@Service
@Transactional(rollbackFor = DataAccessException.class)
public class DataAnalysisServiceImpl implements DataAnalysisService {
    private static Logger logger = LoggerFactory.getLogger(DataAnalysisServiceImpl.class);
    @Autowired
    DataVDao dao;

    /**
     * 解析前台条件
     * @param dataAnalyseQueryVO
     * @return
     */
    private DataAnalyseQueryVO parseConditionVO(DataAnalyseQueryVO dataAnalyseQueryVO){

        //咨询时间
        if(null!=dataAnalyseQueryVO.getStartTimeEnd()&&dataAnalyseQueryVO.getStartTimeEnd()!=""){
            dataAnalyseQueryVO.setStartTimeEnd(dataAnalyseQueryVO.getStartTimeEnd()+" 23:59:59");
        }
        //首次咨询时间
        if(null!=dataAnalyseQueryVO.getFirstTimeEnd()&&dataAnalyseQueryVO.getFirstTimeEnd()!=""){
            dataAnalyseQueryVO.setFirstTimeEnd(dataAnalyseQueryVO.getFirstTimeEnd()+" 23:59:59");
        }
        //末次咨询时间
        if(null!=dataAnalyseQueryVO.getLastTimeEnd()&&dataAnalyseQueryVO.getLastTimeEnd()!=""){
            dataAnalyseQueryVO.setLastTimeEnd(dataAnalyseQueryVO.getLastTimeEnd()+" 23:59:59");
        }
        //创建时间
        if(null!=dataAnalyseQueryVO.getCreateTimeEnd()&&dataAnalyseQueryVO.getCreateTimeEnd()!=""){
            dataAnalyseQueryVO.setCreateTimeEnd(dataAnalyseQueryVO.getCreateTimeEnd()+" 23:59:59");
        }
        return dataAnalyseQueryVO;
    }
    @Override
    public PageVo findDataList(DataAnalyseQueryVO dataAnalyseQueryVO, PageVo pageVo) {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        Page<DataAnalyse> page= PageHelper.startPage(pageVo.getPageNum(),pageVo.getPageSize());
        List<DataAnalyse> vos= dao.getDataVList(dataAnalyseQueryVO);
        int pageIndex=pageVo.getPageNum();
        int size =pageVo.getPageSize();
        for (int i=1;i<=vos.size();i++){
            vos.get(i-1).setRowno(i+(pageIndex-1)*size);
            vos.set(i-1,vos.get(i-1));
            try {
                if(null!= vos.get(i-1).getUname()  ){
                    vos.get(i-1).setUname(URLDecoder.decode(vos.get(i-1).getUname(),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        pageVo.setResult(vos);
        return pageVo;
    }
    @Override
    public PageVo findDataListXls(DataAnalyseQueryVO dataAnalyseQueryVO, PageVo pageVo) {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        List<DataAnalyse> vos= dao.getDataVList(dataAnalyseQueryVO);
        for (int i=1;i<=vos.size();i++){
            try {
                if(null!= vos.get(i-1).getUname()  ){
                    vos.get(i-1).setUname(URLDecoder.decode(vos.get(i-1).getUname(),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

          //  vos.get(i-1).setCallStatusStr();

        }
        pageVo.setResult(vos);
        return pageVo;
    }
    /**
     * 查询汇总
     *
     * @param dataAnalyseQueryVO
     * @param pageVo
     * @return
     */
    @Override
    public PageVo findDataSum(DataAnalyseQueryVO dataAnalyseQueryVO, PageVo pageVo) {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        DataAnalyseSum sumvo= dao.selectSum(dataAnalyseQueryVO);
        ArrayList<DataAnalyseSum> sumvos=new ArrayList<DataAnalyseSum>();
        sumvos.add(sumvo);
        pageVo.setResult(sumvos);
        return pageVo;
    }

    /**
     * 统计页码
     * @param dataAnalyseQueryVO
     * @return
     */
    @Override
    public int  count(DataAnalyseQueryVO dataAnalyseQueryVO)
    {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        return dao.pageCount(dataAnalyseQueryVO);
    }


    /**
     * 获取数据分析查询的通话记录
     *
     * @param dataAnalyseQueryVO
     * @return
     */
    @Override
    public List<DataAnalyse> getCallList(DataAnalyseQueryVO dataAnalyseQueryVO) {

        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        dataAnalyseQueryVO.setXlsgroup("call");
        List<DataAnalyse> vos= dao.getDataVList(dataAnalyseQueryVO);
        if(null!=vos&&vos.size()>0)
        for (int i=1;i<=vos.size();i++){
            try {
                if(null!= vos.get(i-1).getUname()  ){
                    vos.get(i-1).setUname(URLDecoder.decode(vos.get(i-1).getUname(),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            //  vos.get(i-1).setCallStatusStr();

        }
        return vos;
    }

    /**
     * 数据分析中未服务的律师
     *
     * @param dataAnalyseQueryVO
     * @return
     */
    @Override
    public List<DataAnalyse> getLawyerList(DataAnalyseQueryVO dataAnalyseQueryVO) {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        dataAnalyseQueryVO.setXlsgroup("lawyer");

        List<DataAnalyse> vos= dao.getDataVList(dataAnalyseQueryVO);
        if(null!=vos&&vos.size()>0)

            for (int i=1;i<=vos.size();i++){
            try {
                if(null!= vos.get(i-1).getUname()  ){
                    vos.get(i-1).setUname(URLDecoder.decode(vos.get(i-1).getUname(),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return vos;
    }

    /**
     * 数据分析中未使用服务的客户
     *
     * @param dataAnalyseQueryVO
     * @return
     */
    @Override
    public List<DataAnalyse> getClientList(DataAnalyseQueryVO dataAnalyseQueryVO) {
        dataAnalyseQueryVO= parseConditionVO(dataAnalyseQueryVO);
        dataAnalyseQueryVO.setXlsgroup("client");

        List<DataAnalyse> vos= dao.getDataVList(dataAnalyseQueryVO);
        if(null!=vos&&vos.size()>0)

            for (int i=1;i<=vos.size();i++){
            try {
                if(null!= vos.get(i-1).getUname()  ){
                    vos.get(i-1).setUname(URLDecoder.decode(vos.get(i-1).getUname(),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            //  vos.get(i-1).setCallStatusStr();

        }
        return vos;
    }
}
